<?php
declare (strict_types=1);

namespace app\common\tool\account;

use think\facade\Event;

class Account
{
    /**
     * @var string 会员ID
     */
    public $uid;

    /**
     * @var string 类型，此笔明细是什么类型，1=>普通流水 2=>奖金 3=>后台充值
     */
    public $type;

    /**
     * @var string 账户，支持多个
     */
    public $account;

    /**
     * @var string 此笔明细说明
     */
    public $intro;

    /**
     * @var string 此笔明细金额
     */
    public $amount;

    /**
     * @var string 此笔明细来源，多元化可以是来源会员ID，或者订单号
     */
    public $source;

    /**
     * @var string 此笔明细是 + 还是 -
     */
    public $fx;

    /**
     * @var string 此笔明细说明
     */
    public $msg;

    public $is_grant = 2;

    public $is_user;

    public $category;

    public $order_id = 0;

    /**
     * @var null 定义单例
     */
    private static $_instance = null;


    public static function getInstance()
    {
        if (empty(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * @param $uid - 会员ID
     * @param $account - 账户
     * @param $amount - 金额
     * @param $intro - 说明
     * @param $fx - '+' or '-'
     * 原点 2022年3月5日09:42:58 5个基础参数，必传
     */
    public function basics($uid, $account, $amount, $intro, $fx)
    {
        $this->uid = $uid;
        $this->account = $account;
        $this->amount = $amount;
        $this->intro = $intro;
        $this->fx = $fx;
        return $this;
    }

    /**
     * @param int $source - 来源会员ID
     * @param bool $update - 是否更新账户
     * @return bool
     * 原点 2022年3月5日10:11:07 奖金流水
     */
    public function createBonus($source = 0, $category = 0, $update = true): bool
    {
        try {
            $this->source = $source;
            $this->category = $category;
            $this->type = 2;
            $this->is_user = $source ? 1 : 0;
            $this->create($update);
            return true;
        } catch (\Exception $e) {
            throw new AccountException($e->getMessage(), '0');
        }
    }

    public function createRecharge($update = true): bool
    {
        try {

            $this->type = 3;
            $this->create($update);
            return true;
        } catch (\Exception $e) {
            throw new AccountException($e->getMessage(), '0');
        }
    }

    public function createNormal($source = 0, $update = true): bool
    {
        try {
            $this->source = $source;
            $this->type = 1;
            $this->category = 0;
            $this->create($update);
            return true;
        } catch (\Exception $e) {
            throw new AccountException($e->getMessage(), '0');
        }
    }


    public function create($update)
    {
        $this->update = $update;
        Event::trigger('AccountHandle', $this);
    }

    public function setType($val)
    {
        $this->type = $val;
        return $this;
    }

    public function setUid($val)
    {
        $this->uid = $val;
        return $this;
    }

    public function setAccount($val)
    {
        $this->account = $val;
        return $this;
    }

    public function setIntro($val)
    {
        $this->intro = $val;
        return $this;
    }

    public function setAmount($val)
    {
        $this->amount = $val;
        return $this;
    }

    public function setSource($val)
    {
        $this->source = $val;
        return $this;
    }

    public function setFx($val)
    {
        $this->fx = $val;
        return $this;
    }

    public function setMsg($val)
    {
        $this->msg = $val;
        return $this;
    }

    public function setIsGrant($val)
    {
        $this->is_grant = $val;
        return $this;
    }

    public function setIsUser($val)
    {
        $this->is_user = $val;
        return $this;
    }

    public function setCategory($val)
    {
        $this->category = $val;
        return $this;
    }

    public function setOrder($val)
    {
        $this->order_id = $val;
        return $this;
    }

    /**
     * @param $name
     * @param $val
     * 魔术方法
     */
    public function __call($name, $val)
    {

        $this->$name = $val;
        return $this;
    }

}